// rgTriangle.cpp

#include "../../include/kernel/rgTriangle.h"
/*
void rgTriangle::calculeCenter()
{
	mCenter = rgVector::vZero;

	for(int i = 0; i < 3; ++i)
	{
		mCenter.x += mVertex[i].x;
		mCenter.y += mVertex[i].y;
		mCenter.z += mVertex[i].z;
	}

	mCenter /= 3;
}


bool rgTriangle::intersect(rgRay& inRay)
{
	float A, B, distance;
	bool ret = rgFALSE;

	if( (A = inRay.direction * mNormal) != 0.0f )
	{
		B = -(mNormal * inRay.origin) + mPlaneDistance;
		distance = B / A;

		if (distance > 1.0f)
		{
			inRay.distance = distance;
			inRay.findDestination();

			ret = tomasBen(inRay);

			return ret;
		}
	}

	return rgFALSE;
}

// Tomas and Ben 
bool rgTriangle::tomasBen(rgRay& inRay)
{
	rgVector edge1, edge2, tvec, pvec, qvec;
	rgReal det, iDet;
	rgReal t, u, v;

	edge1 = mVertex[1] - mVertex[0];
	edge2 = mVertex[2] - mVertex[0];

	pvec = inRay.direction ^ edge2;
	det = edge1 * pvec;

	if(det > -0.000001f && det < 0.000001f) return rgFALSE;

	iDet = 1.0f / det;

	tvec = inRay.origin - mVertex[0];

	u = (tvec * pvec) * iDet;

	if (u < 0.0f || u > 1.0f) return rgFALSE;

	qvec = tvec ^ edge1;

	v = (inRay.direction * qvec) * iDet;

	if (v < 0.0f || u + v > 1.0f) return rgFALSE;

	t = (edge2 * qvec) * iDet;

	return rgTRUE;
}
*/

